package server;

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class IPTablesTest {

	static IPTablesComm iptables = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		iptables = new IPTablesComm();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testFlush() {
		try {
			iptables.flush();
		} catch (Exception e) {
			fail("Couldn't flush iptables rules");
		}
	}

	@Test
	public void testAdd() {
		try {
			iptables.flush();
		} catch (Exception e) {
			fail("Couldn't flush iptables rules");
		}
		Rule rule = new Rule(1, null, 6667, Rule.permissionOpt.ALLOW);
		try {
			iptables.add(RulesTableType.IN, rule, true, null);
		} catch (Exception e) {
			fail("Couldn't add iptable rule");
		}
	}
	
	@Test
	public void testAddTwice() {
		try {
			iptables.flush();
		} catch (Exception e) {
			fail("Couldn't flush iptables rules");
		}
		Rule rule = new Rule(1, null, 6667, Rule.permissionOpt.ALLOW);
		try {
			iptables.add(RulesTableType.IN, rule, true, null);
		} catch (Exception e) {
			fail("Couldn't add iptable rule");
		}
	}

	@Test
	public void testRemove() {
		try {
			iptables.flush();
		} catch (Exception e) {
			fail("Couldn't flush iptables rules");
		}
		Rule rule = new Rule(1, null, 6667, Rule.permissionOpt.ALLOW);
		try {
			iptables.add(RulesTableType.IN, rule, true, null);
		} catch (Exception e) {
			fail("Couldn't add iptable rule");
		}
		try {
			iptables.remove(RulesTableType.IN, 1);
		} catch (Exception e) {
			fail("Couldn't delete iptable rule");
		}

	}

}
